package org.jeecg.modules.xxgx.djgl.vo;


import java.util.Map;
import java.util.Objects;

public class Ret<T> {

	private String message;

	private String renson;

	private Integer code;

	private T data;

	private static final String DEFAULT_SUCCESS_Msg = "执行成功";

	private static final String DEFAULT_FAILURE_Msg = "执行失败";

	private static final String EXECTING_MSG="任务执行中,稍后重试或等待回调";

	public static final Integer RETRY_CODE=1;

	public Ret() {

	}

	public Ret(Integer code) {
		this.code = code;
	}

	public static <T> Ret<T> convert(Map param){
		int code = (param.get("code") == null || "".equals(param.get("code")))?-1:Integer.parseInt((String)param.get("code"));
		String msg = (param.get("msg") == null || "".equals(param.get("msg")))?"":(String)param.get("msg");
		T data = param.get("data") == null ? null:(T)param.get("data");
		return new Ret<T>(code).setMessage(msg).setData(data);
	}

	public static <T> Ret<T> convert2(Map param){
		int code = (param.get("code") == null || "".equals(param.get("code")))?-1:Integer.parseInt((String)param.get("code"));
		String msg = (param.get("message") == null || "".equals(param.get("message")))?"":(String)param.get("message");
		T data = param.get("data") == null ? null:(T)param.get("data");
		return new Ret<T>(code).setMessage(msg).setData(data);
	}

	public static <T> Ret<T> successMsg(String msg) {
		return new Ret<T>(0).setMessage(msg).setRenson(msg);
	}

	public static <T> Ret<T> success() {
		return new Ret<T>(0).setMessage(DEFAULT_SUCCESS_Msg)
				.setRenson(DEFAULT_SUCCESS_Msg);
	}

	public static <T> Ret<T> success(T data) {
		return new Ret<T>(0).setMessage(DEFAULT_SUCCESS_Msg)
				.setRenson(DEFAULT_SUCCESS_Msg).setData(data);
	}

	public static <T> Ret<T> success(String msg,T data) {
		return new Ret<T>(0).setMessage(msg)
				.setRenson(msg).setData(data);
	}

	public static <T> Ret<T> fail() {
		return new Ret<T>(-1).setMessage(DEFAULT_FAILURE_Msg)
				.setRenson(DEFAULT_FAILURE_Msg);
	}

	public static <T> Ret<T> failMsg(String msg) {
		return new Ret<T>(-1).setMessage(msg);
	}

	public static <T> Ret<T> fail(String renson) {
		return new Ret<T>(-1).setMessage(DEFAULT_FAILURE_Msg).setRenson(renson);
	}

	public static <T> Ret<T> fail(String msg,String renson) {
		return new Ret<T>(-1).setMessage(msg)
				.setRenson(renson);
	}

	public static <T> Ret<T> fail(Integer code,String renson) {
		if (Objects.isNull(code)) {
			return new Ret<T>(-1).setMessage(DEFAULT_FAILURE_Msg)
					.setRenson(DEFAULT_FAILURE_Msg);
		}
		return new Ret<T>(code).setMessage(renson).setRenson(renson);
	}

	public static <T> Ret<T> fail(Integer code,String msg,String renson) {
		if (Objects.isNull(code)) {
			code = -1;
		}
		return new Ret<T>(code).setMessage(msg).setRenson(renson);
	}

	public static <T> Ret<T> other(Integer code,String message) {
		return new Ret<T>(code).setMessage(message).setRenson(message);
	}

	// 任务执行中,稍后重试或等待回调
	public static <T> Ret<T> executing() {
		return new Ret<T>(2).setMessage(EXECTING_MSG).setRenson(EXECTING_MSG);
	}
	public static <T> Ret<T> other(Integer code,String message,String renson) {
		return new Ret<T>(code).setMessage(message).setRenson(renson);
	}

	public String getMessage() {
		return message;
	}

	public Ret<T> setMessageRenson(String message) {
		this.message = message;
		this.renson = message;
		return this;
	}

	public Ret<T> setMessage(String message) {
		this.message = message;
		return this;
	}

	public Integer getCode() {
		return code;
	}

	public Ret<T> setCode(Integer code) {
		this.code = code;
		return this;
	}

	public T getData() {
		return data;
	}

	public Ret<T> setData(T data) {
		this.data = data;
		return this;
	}

	public String getRenson() {
		return renson;
	}

	public Ret<T> setRenson(String renson) {
		this.renson = renson;
		return this;
	}

}

